
require'zdc.doc'
require'zdc.typeex'
require'zdc.node'
require'zdc.sandbox'
require'zdc.dpl'
require'zdc.dal'
require'zdc.gim'
require'zdc.ds.module'
require'zdc.showutil'
require 'zdc.ds.module'
require 'zdc.logger'
require 'zsvc.appbase'
local nodeapi=require 'zdc.nodeapi'

local function docmd(dal,line)
	local cmd,arg = string.match(line,'^%s*(%S+)[ ]*(.*)')
	if not cmd then return end
	local node=dal:SureNode(cmd)
	if not arg  or #arg ==0 then
		return zdc.showutil.ShowNode(node,io.write,true,100)
	else
		return zdc.nodeapi.callact(nil,DAL,node,loadstring('return ' .. arg)())
	end
end

local function _get_fallback_node(dpl,id)
	local node_from_module = zdc.ds.module.GetNodeFromModule(dpl,id,'test.pto')
	if node_from_module then
		return node_from_module
	else
		return nil
	end
end

local DPLCFG={
	DB={
	MODULE='zdc.ds.sqlite3',
	PARAMS={zutil.getpath('DOC') .. 'test'..'.db3'},
	},
	CONFIG_NAME='config',
	ROOT_PROTOTYPE='@root_test',
	_get_fallback_node=_get_fallback_node,
	sbenv={
		require=require
	}
}

local FBDPLCFG={
	DB={MODULE='zdc.ds.fallbackdb'},
	ROOT_PROTOTYPE='@root_test',
	_get_fallback_node=_get_fallback_node,
	sbenv={
		require=require
	}
}

local TESTCFG={
	dpl={
		fallback=FBDPLCFG,
		{'cl','cl/',DPLCFG}
	}
}

function cmdloop()
	while true do
		cmd=(cmd or '') .. io.read('*l')
		if not cmd then break end
		if cmd=='!exit' then break end
		local r,msg,msg2=xpcall(function()return docmd(DAL,cmd)end,debug.traceback)
		if not r then
			io.write(tostring(msg))
		else
			io.write(tostring(msg),'\t',tostring(msg2))
		end
		io.write'\n'
		cmd=nil
	end
end

local app=assert(zsvc.appbase.new(TESTCFG,LOGGER),"can't create APP")

_G[_APP]=app

cmdloop()

